class Main extends egret.DisplayObjectContainer {		
	private _myLoader:GYLite.GYLoader;	
	private _dragLock:boolean;
	public static instance:Main;		
	public globalLoad:any;
	public constructor()
	{
		super();								
		var s = this;		
		s._myLoader = GYLite.GYLoader.getDefaultLoader();
		if(s.stage)
			s.projInit();
		else
			s.addEventListener(egret.Event.ADDED_TO_STAGE, s.addToStage,s);			
	}
	private addToStage(e:egret.Event):void
	{	
		this.projInit();
	}
	private projInit():void
	{
		let s = this;
		//防止移动端拖拽画布产生的问题，可以使用下面这个方法禁止掉
		// s.dragLock(true);	
		//自定义加载进度UI
		s.globalLoad = window["globalLoad"]?window["globalLoad"]:{
			hasLoading:true,
			globalLoadPercent:function(cur,max,label){},
			setPercent:function(cur,max,label){},
			addLoadingUI:function():void{},
			removeLoadingUI:function():void{},
		};
		Main.instance = s;
		s.removeEventListener(egret.Event.ADDED_TO_STAGE, s.addToStage, s);		
		
		GYLite.GYSprite.Init(s.stage, null, null);
		s.loadCfg();
	}	
	public loadCfg():void
	{
		let s = this;		
		//加载主程序配置
		s.myLoader.loadData("configs/default_config.json", s.configLoaded,s,null,GYLite.GYLoader.TYPE_JSON,"GET");	
	}
	public get myLoader():GYLite.GYLoader
	{var s = this;
		return s._myLoader;
	}	
	private configLoaded(loadInfo:GYLite.LoadInfo):void
	{
		let s = this;
		if(loadInfo.content == null)
		{
			console.error("加载配置失败!");
			return;
		}
		let cfg:any = loadInfo.content.res;
		if(cfg.layoutMode)
			s.stage.scaleMode = cfg.layoutMode;
		if(cfg.orientation)
			s.stage.orientation = cfg.orientation;
		//加载主程序资源
		s.myLoader.loadData("default_res",s.resLoadComp,s,s.resProg,GYLite.GYLoader.TYPE_BINARY,"GET");		
		
	}
	private resProg(e:egret.Event):void{
		let l:GYLite.SeqURLLoader = e.currentTarget;	
		let s= this;					
		s.globalLoad.globalLoadPercent(l.progressData.loaded, l.progressData.total, "加载主程序资源");
	}       
	private resLoadComp(loadInfo:GYLite.LoadInfo):void
	{var s = this;			
		if(loadInfo.content == null)
		{			
			return;
		}					
		let l:GYLite.CompressLoadInfo = s._myLoader.loadGYCompressBytes(loadInfo.content.res,s.resUncompressComp,s,null,s.resUncompressPorg);
	}
	private resUncompressPorg(l:GYLite.CompressLoadInfo):void		
	{
		// GYLite.LoadUI.getInstance().setProg(l.resMax - l.resCount, l.resMax, "解压中...");
		// Log.writeLog(MultiLang.str66 + (l.resMax - l.resCount) + "/" + l.resMax + "-" + l.progressInfo[l.progressInfo.length - 1].key,Log.VERBOSE);
	}
	private resUncompressComp(l:GYLite.CompressLoadInfo):void
	{let s = this;
		
		if(l.errorCode > 0)			
			return;
		if(l.resCount == 0)					
			s.initUI();
	}
	private initUI():void
	{var s = this;			
		GYLite.GYSprite.skinTheme = new Win7SkinTheme;
		s.globalLoad.removeLoadingUI();
		let txt:GYLite.GYText = new GYLite.GYText;
		txt.width = s.stage.stageWidth;
		txt.height = 65;
		txt.size = 60;
		txt.x = s.stage.stageWidth - txt.width >> 1;
		txt.y = (s.stage.stageHeight >> 1) - 60;
		txt.align = "center"
		txt.text = "欢迎使用羔羊引擎!"
		s.addChild(txt);
	}	
	public dragLock(val:boolean):void
	{let s = this;
		if(s._dragLock == val)return;
		s._dragLock = val;
		if(val)
			document.addEventListener("dragstart",s.dragFunc);
		else
			document.removeEventListener("dragstart",s.dragFunc);
	}
	private dragFunc(event):void
	{
		event.preventDefault();
	}	
}